#include <iostream>
#include <vector>
#include <thread>
#include <chrono>

#include "config/config_manager.h"
#include "core/monitor_manager.h"
#include <glog/logging.h>

using namespace sysinfo::monitor;

int main(int argc, char* argv[]) {
    // 初始化glog
    google::InitGoogleLogging(argv[0]);
    FLAGS_logtostderr = 1; // 将日志输出到stderr

    if (argc < 2 || argc > 3) {
        LOG(ERROR) << "Usage: " << argv[0] << " <config_file> [duration_seconds]";
        LOG(ERROR) << "  duration_seconds: Run duration in seconds, 0 for infinite (default: 0)";
        return 1;
    }

    std::string config_file = argv[1];
    int duration_seconds = 5; // 默认无限运行
    
    if (argc == 3) {
        try {
            duration_seconds = std::stoi(argv[2]);
        } catch (const std::exception& e) {
            LOG(ERROR) << "Invalid duration value: " << argv[2];
            return 1;
        }
    }

    MonitorManager monitor_manager;
    if (!monitor_manager.Init(config_file)) {
        LOG(ERROR) << "Failed to initialize monitor manager";
        return 1;
    }

    // 打印初始化时队列的大小
    // std::cout << "Initial queue sizes - Main queue: " << monitor_manager.GetMainQueueSize() 
    //           << ", Circular queue: " << monitor_manager.GetCircularQueueSize() << std::endl;

    // 启动监控系统
    monitor_manager.Start();

    // 打印启动后队列的大小
    // std::cout << "After start queue sizes - Main queue: " << monitor_manager.GetMainQueueSize() 
    //           << ", Circular queue: " << monitor_manager.GetCircularQueueSize() << std::endl;

    // 执行一次性任务
    // LOG(INFO) << "==================Starting once...";
    monitor_manager.StartOnce();
    // LOG(INFO) << "==================Finished once.";
    // 打印执行一次性任务后队列的大小
    // std::cout << "After StartOnce queue sizes - Main queue: " << monitor_manager.GetMainQueueSize() 
    //           << ", Circular queue: " << monitor_manager.GetCircularQueueSize() << std::endl;

    if (duration_seconds == 0) {
        // 持续运行直到收到中断信号
        LOG(INFO) << "Running indefinitely. Press Ctrl+C to stop.";
        while (true) {
            std::this_thread::sleep_for(std::chrono::seconds(1));
            
            // 获取一批数据
            auto batch = monitor_manager.FetchResultBatchByQueueSize();
            LOG(INFO) << "Collected " << batch.size() << " data items from main queue";

            auto circular_batch = monitor_manager.FetchResultBatchByQueueSizeFromCircular();
            LOG(INFO) << "Collected " << circular_batch.size() << " data items from circular queue";
        }
    } else {
        // 等待指定时间收集数据
        // LOG(INFO) << "+++++++++++++ Running for " << duration_seconds << " seconds.";
        std::this_thread::sleep_for(std::chrono::seconds(duration_seconds));
        // LOG(INFO) << "+++++++++++++ Collecting data.";
        // 获取一批数据
        auto batch = monitor_manager.FetchResultBatchByQueueSize();
        LOG(INFO) << "Collected " << batch.size() << " data items from main queue";

        auto circular_batch = monitor_manager.FetchResultBatchByQueueSizeFromCircular();
        LOG(INFO) << "Collected " << circular_batch.size() << " data items from circular queue";
    }

    // 停止监控系统
    monitor_manager.Stop();

    // 关闭glog
    google::ShutdownGoogleLogging();

    return 0;
}